package com.uuticketweb.ticket.user.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.uuticketweb.ticket.base.GlobalConstants;
import com.uuticketweb.ticket.db.HqlUtils;

public class UserDao extends HibernateDaoSupport {
	private static final Logger LOG = Logger.getLogger(UserDao.class);
	
	public List searchUser(int type, String searchword) {
		StringBuffer con = new StringBuffer();
		if(type != GlobalConstants.ALL) {
			con.append(" userDetail.type=:type ");
		}
		if(!StringUtils.isBlank(searchword)) {
			HqlUtils.appendAnd(con);
			con.append(" (userDetail.accountName like :searchword or userDetail.realName like :searchword or userDetail.mobilePhone like :searchword or userDetail.email like :searchword) "); 
		}
		HqlUtils.insertWhere(con);
		
		Session session = getSession();
		Query query = session.createQuery("from UserDetail userDetail " + con);
		if(type != GlobalConstants.ALL) {
			query.setInteger("type", type);
		}
		if(!StringUtils.isBlank(searchword)) {
			query.setString("searchword", searchword);
		}
		return query.list();
	}
}
